#include "SqliteDataSourceTest.h"
#include <ctime>
#include "common/Date.h"
#include "db/DataSource.h"
#include "db/DataSourceException.h"
#include "db/DataSourceManager.h"
#include "genealogy_tree/Person.h"
#include "genealogy_tree/Gender.h"
#include "genealogy_tree/Photo.h"

namespace etu {

using etu::common::Date;
using etu::db::DataSource;
using etu::db::DataSourceException;
using etu::db::DataSourceManager;
using etu::genealogy_tree::Person;
using etu::genealogy_tree::Gender;
using etu::genealogy_tree::Photo;

void SqliteDataSourceTest::setUp() {
    this->treeName = "testTree";
    this->tree = new GenealogyTree();

    Person* first = new Person("First", "Last", etu::genealogy_tree::MALE_GENDER, Date::now(),
            Date::unkown());
    Person* second = new Person("Second", "Last", etu::genealogy_tree::MALE_GENDER, Date::now(),
            Date::unkown());

    tree->setRoot(first);
    tree->addChild(first, second);

    this->photo = new Photo("test.jpg");
    this->person = new Person("Photo", "Person", etu::genealogy_tree::MALE_GENDER, Date::now(),
            Date::unkown(), "", this->photo->getHash());
}

void SqliteDataSourceTest::tearDown() {
    delete this->tree;
    delete this->person;
    delete this->photo;
}

void SqliteDataSourceTest::insertGenealogyTest() {
    DataSource* dataSource = DataSourceManager::getDataSource();

    try {
        if(!dataSource->isGenealogyTreeExists(this->treeName)) {
            dataSource->addGenealogyTree(this->treeName, (* this->tree));
        }
    } catch(const DataSourceException& e) {
        CPPUNIT_FAIL(e.what());
    }
}

void SqliteDataSourceTest::updateGenealogyTest() {
    DataSource* dataSource = DataSourceManager::getDataSource();

    try {
        if(dataSource->isGenealogyTreeExists(this->treeName)) {
            GenealogyTree* tree = dataSource->getGenealogyTree(this->treeName);
            Person* child = new Person("New", "Child", etu::genealogy_tree::FEMALE_GENDER,
                    Date::now(), Date::unkown());
            tree->addChild(tree->getRoot(), child);
            dataSource->updateGenealogyTree(this->treeName, (* tree));
            delete tree;
        }
    } catch(const DataSourceException& e) {
        CPPUNIT_FAIL(e.what());
    }
}

void SqliteDataSourceTest::deleteGenealogyTest() {
    DataSource* dataSource = DataSourceManager::getDataSource();

    try {
        dataSource->deleteGenealogyTree(this->treeName);
    } catch(const DataSourceException& e) {
        CPPUNIT_FAIL(e.what());
    }
}

void SqliteDataSourceTest::insertPhotoTest() {
    DataSource* dataSource = DataSourceManager::getDataSource();

    try {
        if(!dataSource->isPhotoExists(* this->person)) {
            dataSource->addPhoto(this->person, * this->photo);
        }
    } catch(const DataSourceException& e) {
        CPPUNIT_FAIL(e.what());
    }
}

void SqliteDataSourceTest::updatePhotoTest() {
    // TODO
}

void SqliteDataSourceTest::deletePhotoTest() {
    DataSource* dataSource = DataSourceManager::getDataSource();

    try {
        dataSource->deletePhoto(* this->person);
    } catch(const DataSourceException& e) {
        CPPUNIT_FAIL(e.what());
    }
}

}
